var app=new Vue({
    el:'.mobileapp',
    computed:{
        exchangenum:{//兑换数量
            get(){
                if(!isNaN(Number(this.exchange.exchangenum))){
                    return this.exchange.exchangenum
                }else{
                    return 0
                }
            },
            set(v){ 
                if(!isNaN(Number(v))){
                    if(parseFloat(Number(v))<0){
                        this.exchange.exchangenum=0
                    }else{
                        this.exchange.exchangenum=parseFloat(v)
                    }
                    if(parseFloat(Number(v))>parseFloat(this.userinfo.banlance)){
                        this.exchange.exchangenum=parseInt(this.userinfo.banlance)
                    }
                    let computedresult=parseFloat(Number(this.exchange.exchangenum))*this.exchange.agratenum
                    if(!isNaN(Number(computedresult))){
                        this.exchange.exchangeagcoin=computedresult
                    }else{
                        this.exchange.exchangeagcoin=0
                    }
                }else{
                    this.cashout.exchangeagcoin=0
                }
            }
        },
        cashoutnum:{ //兑出数量
            get(){
                if(!isNaN(Number(this.cashout.exchangenum))){
                    if(parseFloat(this.cashout.exchangenum)>=0){
                        return this.cashout.exchangenum
                    }else{
                        return 0
                    }
                }else{
                    return 0
                }
            },
            set(v){ 
                if(!isNaN(Number(v))){
                    if(parseFloat(Number(v))<0){
                        this.cashout.exchangenum=0
                    }else{
                        this.cashout.exchangenum=parseInt(v)
                    }
                    if(parseFloat(Number(v))>parseFloat(this.userinfo.myagbanlance)){
                        this.cashout.exchangenum=parseInt(this.userinfo.myagbanlance)
                    }
                    let computedresult=parseFloat(Number(this.cashout.exchangenum))/parseFloat(this.exchange.agratenum)
                    computedresult=parseFloat(computedresult.toFixed(8)) 
                    if(!isNaN(Number(computedresult))){
                        this.cashout.exchangeagcoin=computedresult
                    }else{
                        this.cashout.exchangeagcoin=0
                    }
                }else{
                    this.cashout.exchangeagcoin=0
                }
            }
        }
    },
    created(){
        if(localStorage.getItem('ID')!=null||localStorage.getItem('ID')!=''){
            this.getagBalance()
        }
    },
    data(){
        return {
            HPS: "HPS",
            ETH: "ETH",
            BTC: "BTC",
            HSR: "HSR",
            HAND: "HAND",
            HART: "HART",
            MOT: "MOT",
            showtransforminag:true,//兑换AG分
            showtransformout:false,//兑出AG分
            showText: false,
            hideText: true,
            isshowexchange:false, //是否再开兑换兑出功能
            exchange:{ //兑换配置项
                defaultcoin:'HPS', //默认币种
                isshowcoinarr:false, //是否展示下拉选
                exchangenum:'0', //兑换数量
                exchangeagcoin:'0',//兑换后数量
                agratenum:5, //AG兑换比例
                disabledbtn:false //禁用兑换点击按钮
            },
            cashout:{ //兑出配置项
                exchangenum:'0', //兑换数量
                exchangeagcoin:'0',//兑换后币种数量
                disabledbtn:false //禁用兑出点击按钮
            },
            userinfo:{ //用户信息
                banlance:'',//用户余额
                myagbanlance:0,//我的ag积分余额
            },
            information:""
        }
    },
    methods:{
        filtertime(num){ //过滤时间
            let times=new Date(num)
            let month=times.getMonth()+1
            let day=times.getDate()
            if(month<10){
                month='0'+month
            }
            if(day<10){
                day='0'+day
            }
            return `${month}-${day}`
        },
        getagBalance(){//获取当前用户AG分
            let params={
                coin:this.exchange.defaultcoin
            }
            axios.post('/ag/agBalance',params).then((res) => {
                if(res.data.code==200){
                    if(res.data.data){
                        this.userinfo.myagbanlance=res.data.data
                    }
                }
            })
        },
        changetransform(keyname){ //用户点击兑换、兑出AG分栏目
            if(!keyname){
                return 
            }
            this.$data[keyname]=!this.$data[keyname]
            if(keyname=='showtransforminag'){ //兑换AG分
                this.showtransformout=false
                if(this.$data[keyname]){
                    this.getagDict(this.exchange.defaultcoin) //根据币种换算兑换比例
                }
            }else if(keyname=='showtransformout'){//兑出AG分
                this.showtransforminag=false
            }
        },
        changeexchangecoin(cointype,banlance){//用户点击更换币种
            this.exchange.defaultcoin=cointype
            this.exchange.isshowcoinarr=false
            this.userinfo.banlance=banlance
            this.getagDict(cointype)
            this.getagBalance()
        },
        getagDict(coinname){ //选择币种获取兑换比例
            if(!coinname){
                return 
            }
            axios.get(`/ag/dict/${coinname}`,{}).then((res) => {
                this.exchange.agratenum=parseFloat(res.data.data.dictValue)
            })
        },
        confirmexchange(){ //确定兑换AG分
            //为空、NaN、兑换数量>用户余额
            if(
                this.exchange.exchangenum==''||isNaN(Number(this.exchange.exchangenum))||this.exchange.exchangenum==0||
                parseFloat(Number(this.exchange.exchangenum))>parseFloat(this.userinfo.banlance)
            ){
                return 
            }
            this.exchange.disabledbtn=true
            let params={
                userId:parseInt(localStorage.getItem('ID')),
                coin:this.exchange.defaultcoin,      
                amount:parseFloat(this.exchange.exchangenum)
            }
            axios.post('/ag/account/buyAgCredit',params).then((res) => {
                this.exchange.disabledbtn=false
                if(res.data.code==200){
                    if(!this.getCookie('firsttransforminag')){
                        this.addCookie('firsttransforminag',new Date().getTime(),'Infinity')
                    }
                    this.getagBalance()
                }
            })
        },
        getCookie(cookiename){ //获取cookie
            var arr,reg=new RegExp("(^| )"+cookiename+"=([^;]*)(;|$)");
            if(arr=document.cookie.match(reg)){
                return unescape(arr[2]);
            }else{
                return null;
            }
        },
        addCookie(objName,objValue,objDays){ //设置cookie
            var str = objName + "=" + escape(objValue);
            if(objDays > 0){
                var date = new Date();
                var ms = objDays*24*3600*1000;
                date.setTime(date.getTime() + ms);
                str += "; expires=" + date.toGMTString();
            }
            if(objDays===Infinity){
                str += "; expires=Fri, 31 Dec 9999 23:59:59 GMT";
            }
            str += "; path=/";
            document.cookie = str;
        },
        enteraggame(gameType=null){ //点击进入游戏
            //无token||无ID
            if(
                // isNaN(Number(this.userinfo.myagbanlance))||
                // parseFloat(this.userinfo.myagbanlance)<=0||
                localStorage.getItem('mytoken')==null||
                localStorage.getItem('mytoken')==''||
                localStorage.getItem('ID')==''||
                localStorage.getItem('ID')==''
            ){
                location.href = '/singin'
                return 
            }
            // 我的ag积分余额小于0||非数字||
            if( 
                isNaN(Number(this.userinfo.myagbanlance))||
                parseFloat(this.userinfo.myagbanlance)<=0
            ){
                let lang = this.getCookie("lang");
                let timer=setInterval(() => {
                     document.getElementsByClassName('errorMessage')[0].className='errorMessage'
                     clearInterval(timer)
                     timer=null
                 },2000)
                 document.getElementsByClassName('errorMessage')[0].className='errorMessage fadeInUp'
                 if (lang == "en_US") {
                     this.information = "Please recharge AG points"
                 } else if (lang == "zh_TW") {
                     this.information = "請充值AG分"
                 } else {
                     this.information = "请充值AG分"
                 }
                return 
            }
            let params={
                coin:this.exchange.defaultcoin,
                gameType:gameType?gameType:8, //AG大厅
                token:localStorage.getItem('mytoken'), //用户令牌
                isMobile:false //pc
            }
            axios.post('/ag/getGameUrl',params).then((res) => {
                if(res.data.code==200){
                    let form=document.createElement('form')
                    form.setAttribute('method','post')
                    form.setAttribute('class','gotoagGame')
                    form.setAttribute('action',res.data.data)
                    // form.setAttribute('target','_blank')
                    document.body.appendChild(form)
                    document.getElementsByClassName('gotoagGame')[0].submit()
                    document.body.removeChild(document.getElementsByClassName('gotoagGame')[0])
                }
            })
        },
        confircashout(){ //从AG兑回
            if(
                localStorage.getItem('ID')==''||
                localStorage.getItem('ID')==''
            ){
                location.href = '/singin'
                return 
            }
            if(
                this.userinfo.myagbanlance==''||
                this.cashout.exchangenum==0||
                this.cashout.exchangenum==''||
                isNaN(Number(this.userinfo.myagbanlance))||
                parseFloat(this.userinfo.myagbanlance)<=0
            ){
                return 
            }
            this.cashout.disabledbtn=true
            let params={
                coin:this.exchange.defaultcoin,
                amount:parseFloat(this.cashout.exchangenum) 
            }
            axios.post('/ag/transferFromAg',params).then((res) => {
                this.cashout.disabledbtn=false
                if(res.data.code==200){
                    if(res.data.data){
                        this.getagBalance()
                    }
                }
            })
        }
    }
})
